//
//  ViewController.m
//  08-UItableView编辑下
//
//  Created by GivenBmli on 16/7/26.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
#define  kTableViewCellReuse @"reuse"
@interface ViewController () <UITableViewDelegate , UITableViewDataSource>
@property (nonatomic , strong)NSMutableArray * nameArray;
@property (nonatomic , strong)UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:arr];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
    // 实现多选
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

#pragma mark - tableView Delegate
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return @"不能删除";
    }
    return @"删除";
}

- (NSArray <UITableViewRowAction *>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"收藏了");
    }];
    
    UITableViewRowAction *rowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSString *name = [[_nameArray objectAtIndex:indexPath.row]copy];
        [_nameArray removeObjectAtIndex:indexPath.row];
        [_nameArray insertObject:name atIndex:0];
        [_tableView reloadData];
    }];

    UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [_nameArray removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UILayoutPriorityDefaultLow];
    }];

    
    rowAction.backgroundColor = [UIColor redColor];
    rowAction1.backgroundColor = [UIColor orangeColor];
    rowAction2.backgroundColor =[UIColor yellowColor];
    return @[rowAction , rowAction1 , rowAction2 ];
}


#pragma mark - tableView DataSource
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
